{% extends 'layout.html' %}

{% block content %}
    <div class="container">


        <div class="panel panel-default">
            <div class="panel-heading">新增任务</div>
            <div class="panel-body">
                <form id="formAdd" novalidate>
                    <div class="clearfix">
                        {% for item in form %}
                            <div class="col-xs-6">
                                <div class="form-group" style="position: relative; margin-bottom: 20px; ">
                                    <label>{{ item.label }}: </label>
                                    {{ item }}
                                    <span class="error-msg"
                                          style="color: red; position: absolute">{{ item.errors.0 }}</span>
                                </div>
                            </div>
                        {% endfor %}
                        <div class="col-xs-12">
                            <button id="btnAdd" type="button" class="btn btn-success">保存</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>

        <div class="panel panel-default">
            <!-- Default panel contents -->
            <div class="panel-heading" style="font-weight: 700">
                <span class="glyphicon glyphicon-th-list" aria-hidden="true"></span>
                管理员账户列表
            </div>

            <!-- Table -->

{#            <table class="table table-hover">#}
{#                <thead>#}
{#                <tr>#}
{#                    <th>ID</th>#}
{#                    <th>标题</th>#}
{#                    <th>级别</th>#}
{#                    <th>负责人</th>#}
{#                    <th>操作</th>#}
{#                </tr>#}
{#                </thead>#}
{#                <tbody>#}
{##}
{#                {% for item in queryset %}#}
{#                    <tr>#}
{#                        <td>{{ item.id }}</td>#}
{#                        <td>{{ item.title }}</td>#}
{#                        <td>{{ item.get_level_display }}</td>#}
{#                        <td>{{ item.user.username }}</td>#}
{#                        <td>#}
{#                            <a href="/admin/{{ item.id }}/reset/">重重</a>#}
{#                        </td>#}
{#                        <td>#}
{#                            <a class="btn btn-primary btn-xs" href="/admin/{{ item.id }}/edit/">编辑</a>#}
{#                            <a class="btn btn-danger btn-xs" href="/admin/{{ item.id }}/delete/">删除</a>#}
{#                        </td>#}
{#                    </tr>#}
{#                {% endfor %}#}
{#                </tbody>#}
{#            </table>#}
        </div>


        <hr/>
        <h1>Ajax学习</h1>

        <h3>示例一</h3>
        <input id="btn" type="button" class="btn btn-success" value="点击"/>

        <h3>示例二</h3>
        <input id="txtUser" type="text" placeholder="姓名"/>
        <input id="txtAge" type="text" placeholder="年龄"/>
        <input id="btn2" type="button" class="btn btn-success" value="点击"/>

        <h3>示例二</h3>
        <form id="form3">
            <input type="text" name="user" placeholder="姓名"/>
            <input type="text" name="age" placeholder="年龄"/>
            <input type="text" name="email" placeholder="邮箱"/>
            <input type="text" name="more" placeholder="简绍"/>
        </form>
        <input id="btn3" type="button" class="btn btn-success" value="点击"/>


    </div>
{% endblock %}


{% block js %}
    <script type="text/javascript">
        $(function () {
            // 页面框架加载完成之后代码直接执行
            // 点击发送
            bindBtn1Event();
            // 输入框点击发送到后台
            bindBtn2Event();
            // 表单形式发送到后台
            bindBtn3Event();

            // 案例
            bindBtnAddEvent();

        })

        // 点击发送
        function bindBtn1Event() {
            {#console.log('成功点击');#}
            $("#btn").click(function () {
                $.ajax({
                    url: '/task/ajax/',
                    type: 'post',
                    data: {
                        n1: 123,
                        b2: 456
                    }, success: function (res) {
                        console.log(res);
                    }
                })
            })
        }

        // 输入框点击发送到后台
        function bindBtn2Event() {
            $("#btn2").click(function () {
                $.ajax({
                    url: '/task/ajax/',
                    type: 'post',
                    data: {
                        name: $('#txtUser').val(),
                        age: $('#txtAge').val()
                    },
                    dataType: 'JSON',
                    success: function (res) {
                        console.log(res);
                    }
                })
            })
        }

        // 表单形式发送后台
        function bindBtn3Event() {
            $("#btn3").click(function () {
                $.ajax({
                    url: '/task/ajax/',
                    type: 'post',
                    // 将表单所有数据传到后台
                    data: $('#form3').serialize(),
                    dataType: 'JSON',
                    success: function (res) {
                        console.log(res);
                    }
                })
            })
        }

        function bindBtnAddEvent() {


            $("#btnAdd").click(function () {
                {#点击之前把所有错误信息清空#}
                $(".error-msg").text("")
                $.ajax({
                    url: '/task/add/',
                    type: 'post',
                    // 将表单所有数据传到后台
                    data: $('#formAdd').serialize(),
                    dataType: 'JSON',
                    success: function (res) {
                        {#console.log(res);#}
                        if (res.status) {
                            alert('添加成功');
                        } else {
                            $.each(res.error, function (k, v) {
                                $("#id_" + k).next().text(v[0]);
                            })
                        }
                    }
                })
            });
        }


    </script>

{% endblock %}